<template>
  <div id="addvip">
<!--    <h4>请填写会员信息</h4>-->
    <el-form
      label-position="right"
      style="width: 50%;"
      label-width="100px"
      :model="vipInfo"
      :rules="rules"
      ref="vipInfo">
      <el-form-item
        label="会员名称:"
        prop="name"
        :rules="[{required: true, message: '用户名不能为空'}]">
        <el-input v-model="vipInfo.name"></el-input>
      </el-form-item>
      <el-form-item
        prop="password"
        label="密码:"
      >
        <el-input type="password" v-model="vipInfo.password" placeholder="请输入6-20位数字加英文的组合"></el-input>
      </el-form-item>
      <el-form-item
        prop="password_verify"
        label="确认密码:">
        <el-input type="password" v-model="vipInfo.password_verify" placeholder="再次输入密码"></el-input>
      </el-form-item>
      <el-form-item label="性别:">
        <el-radio-group v-model="vipInfo.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="电话:"
        prop="tel">
        <el-input v-model="vipInfo.tel" placeholder="请输入正确的电话号码"></el-input>
      </el-form-item>
      <el-form-item
        label="身份证号:"
        prop="id_card">
        <el-input v-model="vipInfo.id_card" placeholder="请输入正确的身份证号码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-button type="primary" style="width: 50%" @click="add('vipInfo')">注册</el-button>
          </el-col>
          <el-col :span="12">
            <el-button style="width: 50%" @click="resetForm()">重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    
  </div>
</template>

<script>
  import {mapActions} from 'vuex'
  export default {
    name: "AddVIP",
    data () {
      let validatePassword = (rule, value ,callback)=> {
        let passReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/
        if (!value) {
          callback(new Error('密码不能为空'))
        } else if (!passReg.test(value)) {
          callback(new Error('密码至少包含 数字和英文，长度6-20'))
        } else callback()
      }
      let validateRePassword = (rule, value ,callback)=> {
        if (!value) {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.vipInfo.password) {
          callback(new Error('两次输入的密码不一致'))
        } else callback()
      }
      let  validateTel= (rule, value ,callback)=> {
        const telReg = /^1(3|4|5|6|7|8|9)\d{9}$/
        if (!value) {
          callback(new Error('请输入电话号码'))
        } else if (!(telReg.test(value))) {
          callback(new Error('电话号码格式不正确'))
        } else {
          this.verifyTelAC({tel: this.vipInfo.tel}).then(res => {
            res.bool===true ? callback() : callback(new Error('该电话号码已经注册过了'))
          })
        }
      }
      let  validateIDCard= (rule, value ,callback)=> {
        const idCardReg = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/
        if (!value) {
          callback(new Error('请输入身份证'))
        } else if (!(idCardReg.test(value))) {
          callback(new Error('请输入正确格式的身份证号码'))
        } else {
          this.verifyIdCardAC({idCard: this.vipInfo.id_card}).then(res => {
            console.log('addvipidcard',res)
            res.bool===true ? callback() : callback(new Error('该身份证号已经注册过了'))
          })
        }
      }
      return {
        vipInfo: {
          name: '',
          password: '',
          password_verify: '',
          sex: '男',
          tel: '',
          id_card: ''
        },
        rules: {
          password: [
            {validator: validatePassword, trigger: 'blur'}
          ],
          password_verify: [
            {validator: validateRePassword, trigger: 'blur'}
          ],
          tel: [
            {validator: validateTel, trigger: 'blur'}
          ],
          id_card: [
            {validator: validateIDCard, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      ...mapActions('vipinfo/',['addVipAC','verifyIdCardAC','verifyTelAC']),
      // 注册
      add() {
        console.log('注册',this.vipInfo)
        // this.addVipAC(this.vipInfo).then(res=> {
        //   //切换页面
        // })
        this.$refs.vipInfo.validate(valid => {
          if (valid) {
            let obj = {
              "idCard": this.vipInfo.id_card,
              "name":this.vipInfo.name,
              "password": this.vipInfo.password,
              "sex": this.vipInfo.sex,
              "tel": this.vipInfo.tel
            }
            this.addVipAC(obj).then(res => {
              console.log(res)
              this.$message.success('注册成功')
              this.$router.push('/vipInfo')
            })
          } else {
            this.$message.error('验证不通过,检查信息')
            return
          }
        })
      },
      // 重置
      resetForm() {
        console.log(this.$refs)
        // this.$refs[formName].resetFields()
        this.$refs.vipInfo.resetFields()
      }
    }
  }
</script>

<style lang="less" scoped>
#addvip {
  display: flex;
  justify-content: center;
}
</style>
